from content_center.infrastructure.repository import ContentRepository
from content_center.models import Content, SportLabel, UniversityLabel
from user_center.models import User


class ContentService:
    def __init__(self, content_repository: ContentRepository):
        self.content_repository = content_repository

    def get_contents(self, **kwargs) -> (int, list[dict]):
        total, contents = self.content_repository.get_contents(**kwargs)
        return {
            "total": total,
            "contents": [
                {
                    "id": content.id,
                    "author_name": content.author.username,
                    "author_user_id": content.author.id,
                    "title": content.title,
                    "brief_introduction": content.brief_introduction,
                    "text": content.content_text.replace("\n", "<br>"),
                    "cover": content.cover,
                    "update_time": content.update_time.strftime("%Y-%m-%d %H:%M:%S"),
                    "university_labels": [
                        {"id": label.id, "label_name": label.university_name}
                        for label in content.university_labels.all()
                    ],
                    "sport_labels": [
                        {"id": label.id, "label_name": label.sport_name}
                        for label in content.sport_labels.all()
                    ],
                }
                for content in contents
            ],
        }

    def create_content(
            self,
            user_id: int,
            university_label_ids: list[int] = None,
            sport_label_ids: list[int] = None,
            **kwargs
    ):
        author = User.objects.get(id=user_id)
        university_labels = self.__get_university_labels_by_ids(university_label_ids)
        sport_labels = self.__get_sport_labels_by_ids(sport_label_ids)
        content = Content.objects.create(author=author, **kwargs)
        content.university_labels.set(university_labels)
        content.sport_labels.set(sport_labels)

    def edit_content(
            self,
            content_id: int,
            university_label_ids: list[int] = None,
            sport_label_ids: list[int] = None,
            **kwargs
    ):
        university_labels = self.__get_university_labels_by_ids(university_label_ids)
        sport_labels = self.__get_sport_labels_by_ids(sport_label_ids)
        Content.objects.filter(id=content_id).update(**kwargs)
        content = Content.objects.get(id=content_id)
        content.university_labels.set(university_labels)
        content.sport_labels.set(sport_labels)

    def __get_university_labels_by_ids(
            self, university_label_ids: list[int]
    ) -> list[UniversityLabel]:
        return list(UniversityLabel.objects.filter(id__in=university_label_ids))

    def __get_sport_labels_by_ids(self, sport_label_ids: list[int]) -> list[SportLabel]:
        return list(SportLabel.objects.filter(id__in=sport_label_ids))
